//
//  NSURLSessionConfiguration+DebugManager.m
//  YK_DebugManager
//
//  Created by edward on 2021/1/15.
//

#import "NSURLSessionConfiguration+DebugManager.h"
#import "DebugNSURLProtocol.h"
#import <objc/message.h>

@implementation NSURLSessionConfiguration (DebugManager)

+ (void)load
{
    //因为是类方法所以要调用class_getClassMethod方法进行获取方法的Method
    Method method = class_getClassMethod([self class], @selector(defaultSessionConfiguration));
    Method debugmethod = class_getClassMethod([self class], @selector(debugManager_defaultSessionConfiguration));
    method_exchangeImplementations(method, debugmethod);
    
    Method emethod = class_getClassMethod([self class], @selector(ephemeralSessionConfiguration));
    Method debugemethod = class_getClassMethod([self class], @selector(debugManager_ephemeralSessionConfiguration));
    method_exchangeImplementations(emethod, debugemethod);
    
    Method bmethod = class_getClassMethod([self class], @selector(backgroundSessionConfigurationWithIdentifier:));
    Method debugbmethod = class_getClassMethod([self class], @selector(debugManager_backgroundSessionConfigurationWithIdentifier:));
    method_exchangeImplementations(bmethod, debugbmethod);
}

//返回标准配置，这实际上与NSURLConnection的网络协议栈是一样的，具有相同的共享NSHTTPCookieStorage，共享NSURLCache和共享NSURLCredentialStorage。
+ (NSURLSessionConfiguration *)debugManager_defaultSessionConfiguration{
    NSURLSessionConfiguration *configuration = [self debugManager_defaultSessionConfiguration];
    [configuration addDebugProtocol];
    return configuration;
}

//返回一个预设配置，没有持久性存储的缓存，Cookie或证书。这对于实现像"秘密浏览"功能的功能来说，是很理想的。
+ (NSURLSessionConfiguration *)debugManager_ephemeralSessionConfiguration{
    NSURLSessionConfiguration *configuration = [self debugManager_ephemeralSessionConfiguration];
    [configuration addDebugProtocol];
    return configuration;
}

//独特之处在于，它会创建一个后台会话。后台会话不同于常规的，普通的会话，它甚至可以在应用程序挂起，退出，崩溃的情况下运行上传和下载任务。初始化时指定的标识符，被用于向任何可能在进程外恢复后台传输的守护进程提供上下文。
+ (NSURLSessionConfiguration *)debugManager_backgroundSessionConfigurationWithIdentifier:(NSString *)identifier{
    NSURLSessionConfiguration *configuration = [self debugManager_backgroundSessionConfigurationWithIdentifier:identifier];
    [configuration addDebugProtocol];
    return configuration;
}

- (void)addDebugProtocol {
    if ([self respondsToSelector:@selector(protocolClasses)]
        && [self respondsToSelector:@selector(setProtocolClasses:)]) {
        NSMutableArray * urlProtocolClasses = [NSMutableArray arrayWithArray: self.protocolClasses];
        
        Class doralem = NSClassFromString(@"DoraemonNSURLProtocol");
        if ([urlProtocolClasses containsObject:doralem]) {
            [urlProtocolClasses removeObjectAtIndex:[urlProtocolClasses indexOfObject:doralem]];
            [urlProtocolClasses insertObject:doralem atIndex:0];
            
        }
        
        Class protoCls = DebugNSURLProtocol.class;
        if (![urlProtocolClasses containsObject:protoCls]) {
            [urlProtocolClasses insertObject:protoCls atIndex:0];
        }
        
        
        self.protocolClasses = urlProtocolClasses;
    }
}
@end
